local tianxuan = fk.CreateSkill {
  name = "lingling__tianxuan",
}

Fk:loadTranslationTable{
  ["lingling__tianxuan"] = "天选",
  [":lingling__tianxuan"] = "当你受到致命伤害时，你投掷一个6面骰子，若点数大于3则防止之，然后若点数为6则伤害来源受到2点无来源伤害。",

  ["#lingling__liuxiu_EasterEgg"] = "营头之所堕，其下有覆军，流血三千里。",
}

tianxuan:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(tianxuan.name) and
      data.damage >= player.hp + player.shield
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = math.random(6)
    room:sendLog({
      type = "#lingling__liuxiu_random",
      from = player.id,
      arg = n,
      toast = true,
    })
    if n == 6 and data.from and not data.from.dead then
      local num = 2
      --[[if player.general == "lingling__liuxiu" and data.from.general == "lingling__wangmang" then
        room:sendLog{
          type = "#lingling__liuxiu_EasterEgg",
          from = player.id,
          toast = true,
        }
        num = 3
      end]]--
      room:damage{
        to = data.from,
        damage = num,
        skillName = tianxuan.name,
      }
    end
    if n > 3 then
      data:preventDamage()
    end
  end,
})

tianxuan:addEffect(fk.DamageCaused,{
  can_refresh = function (self, event, target, player, data)
    return data.from == player and player.general == "lingling__liuxiu" and data.to.general == "lingling__wangmang" and data.damageType == fk.FireDamage and player:getMark("lingling__liuxiu_EasterEgg") == 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"lingling__liuxiu_EasterEgg",1)
    data:changeDamage(1)
    player.room:sendLog{
      type = "#lingling__liuxiu_EasterEgg",
      from = player.id,
      toast = true,
    }
  end,
})

return tianxuan
